package com.example.mutichatkotlin

import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.Adapter
import android.widget.Button
import android.widget.EditText
import com.google.gson.Gson
import java.io.DataInputStream
import java.io.DataOutputStream
import java.net.Socket

data class Msg(val username:String, val content:String, var type:Int){
    object Properties{
        val TYPE_RECEIVED=0
        val TYPE_SENT=1
    }
}

class MainActivity : BaseActivity(),View.OnClickListener,Runnable {
    override fun run() {
        try {
            client= Socket("10.0.2.2",8889)
            dis= DataInputStream(client?.getInputStream())
            dos= DataOutputStream(client?.getOutputStream())
            while (true){
                if(!client!!.isClosed&&client!!.isConnected&&!client!!.isInputShutdown){
                    val msgJsonData = dis?.readUTF()
                    val gson=Gson()
                    val msg=gson.fromJson(msgJsonData,Msg::class.java)
                     val message=Message()
                    message.what=Properties.UPDATE_MSGLIST
                    message.obj=msg
                    handler.sendMessage(message)
                }
            }
        }catch (e:java.lang.Exception){
            e.printStackTrace()
        }
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.send->{
                val content:String=inputText.text.toString()
                if(""!=content){
                    val msg=Msg(username,content,Msg.Properties.TYPE_SENT)
                    msgList.add(msg)
                    msgAdapter.notifyItemInserted(msgList.size-1)
                    msgRecyclerView.scrollToPosition(msgList.size-1)
                    inputText.setText("")
                    val gson:Gson=Gson()
                    msgGsonData=gson.toJson(msg)
                    Thread(Runnable {
                        try {
                            dos?.writeUTF(msgGsonData)
                            dos?.flush()
                        }catch (e:Exception){
                            e.printStackTrace();
                        }
                    }).start()
                }
            }
        }
    }

    object Properties{
        val UPDATE_MSGLIST=1
        val TAG="MainActivity"
    }
    private lateinit var send:Button
    private lateinit var msgList:ArrayList<Msg>
    private lateinit var inputText:EditText
    private lateinit var msgRecyclerView:RecyclerView
    private lateinit var msgAdapter:MsgAdapter
    private lateinit var dis:DataInputStream
    private lateinit var dos:DataOutputStream
    private lateinit var client:Socket
    private lateinit var username:String
    private var msgGsonData:String=""

    private var handler:Handler=object :Handler(){
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            when(msg?.what){
                Properties.UPDATE_MSGLIST->{
                    var message:Msg= msg.obj as Msg
                    message.type=Msg.Properties.TYPE_RECEIVED
                    msgList.add(message)
                    msgAdapter.notifyItemInserted(msgList.size-1)
                    msgRecyclerView.scrollToPosition(msgList.size-1)
                }
            }
        }
    }

    companion object {
        fun actionStart(context:Context,username:String){
            val intent:Intent= Intent(context,MainActivity::class.java)
            intent.putExtra("username",username)
            context.startActivity(intent)
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        msgList=ArrayList<Msg>()
        msgAdapter=MsgAdapter(msgList)
        inputText=findViewById<EditText>(R.id.input_text)
        msgRecyclerView=findViewById<RecyclerView>(R.id.msg_recycler_view)
        send=findViewById<Button>(R.id.send)
        val intent:Intent= intent
        username=intent.getStringExtra("username")
        val layoutManager:LinearLayoutManager= LinearLayoutManager(this)
        msgRecyclerView.layoutManager=layoutManager
        msgRecyclerView.adapter=msgAdapter
        send.setOnClickListener(this)
        Thread(this).start()
    }
}
